package oa.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import oa.biz.EmployeeBiz;
import oa.biz.MessageBiz;
import oa.biz.MessageReceptionBiz;
import oa.entity.Employee;
import oa.entity.Message;
import oa.entity.MessageReception;

@Controller
@RequestMapping("/admin")
public class MessageController {
	@Autowired
	private MessageBiz messagebiz;
	@Autowired
	private MessageReceptionBiz messagercpbiz;
	@Autowired
	private EmployeeBiz employeeBiz;
	@RequestMapping("/getMessage")
	public String getMessage(Model model,HttpSession session){
		Employee emp=(Employee) session.getAttribute("employee");
		List<Employee> employee=employeeBiz.getAll();
		int receiverId =emp.getId();
		List<Message> messagelist = messagebiz.getAll(receiverId);
		model.addAttribute("messagelist",messagelist);
		model.addAttribute("employee",employee);
		return "messageList";
	}
	@RequestMapping("/getSaveMessage")
	public String getSaveMessage(Model model,HttpSession session){
		Employee emp=(Employee) session.getAttribute("employee");
		List<Employee> employee=employeeBiz.getAll();
		int creatorId =emp.getId();
		List<Message> messagelist = messagebiz.getAllSave(creatorId);
		model.addAttribute("messagelist",messagelist);
		model.addAttribute("employee",employee);
		return "messageSaveList";
	}
	
	@RequestMapping("/messageupdate")
	public String messageadd(Message message,@Param("receiverId")int receiverId,HttpSession session){
		Employee emp=(Employee) session.getAttribute("employee");
		message.setCreatorId(emp.getId());
		message.setSendTime(new Date(System.currentTimeMillis()));
		message.setStatus(0);
		message.setIsSent(1);
		message.setReceiversId(receiverId);
		messagebiz.messageadd(message);
		return "redirect:/admin/getMessage";
	}
	@RequestMapping("/messagesave")
	public String messagesave(Message message,@Param("receiverId")int receiverId,HttpSession session){
		Employee emp=(Employee) session.getAttribute("employee");
		message.setCreatorId(emp.getId());
		message.setSendTime(new Date(System.currentTimeMillis()));
		message.setStatus(0);
		message.setIsSent(0);
		message.setReceiversId(receiverId);
		messagebiz.messageadd(message);
		return "redirect:/admin/getSaveMessage";
	}
	@RequestMapping("/messagedetail")
	public String messageupdate(Model model,@Param("id")int id){
		model.addAttribute("messagefecthId",messagebiz.fetchById(id));
		return "message-send";
	}
	@RequestMapping("/messagedetailunRead")
	public String messageupdateunRead(Model model,@Param("id")int id){
		MessageReception messagercp =messagercpbiz.fetchByMessageId(id);
		int receiverId=messagercp.getReceiverId();
		int isRead =1;
		messagercpbiz.MessageReceptionupdate(isRead,id,receiverId);
		model.addAttribute("messagefecthId",messagebiz.fetchByIdReaded(id));
		model.addAttribute("messagercpfecthId",messagercpbiz.fetchByMessageId(id));
		model.addAttribute("employee",employeeBiz.getAll());
		return "message-detail";
	}
}
